package com.fruit.service.log.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.fruit.mapper.task.LogMapper;
import com.fruit.pojo.task.TaskLog;
import com.fruit.service.log.TaskLogManager;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class TaskLogService implements TaskLogManager {

	@Autowired
	private LogMapper logMapper;
	
	@Override
	public TaskLog insertTaskLog(String tradeId, String taskName, String startTime) {
		log.info("[{}]-【插入调度日志】-【调度名称：{}】",tradeId,taskName);
		try{
		TaskLog taskLog = new TaskLog();
		taskLog.setTradeId(tradeId);
		taskLog.setTaskName(taskName);
		taskLog.setStartTime(startTime);
		int num = logMapper.insertTaskLog(taskLog);
		if(num == 1){
			log.info("[{}]-【插入调度日志】-【调度名称：{}】-【成功】",tradeId,taskName);
			return taskLog;
		}else{
			log.info("[{}]-【插入调度日志】-【调度名称：{}】-【失败】",tradeId,taskName);
			return null;
		}
		}catch(Exception e){
		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();	
		log.info("[{}]-【插入调度日志】-【调度名称：{}】-【异常】",tradeId,taskName);
		return null;
		}finally{
			log.info("[{}]-【插入调度日志】-【调度名称：{}】-【结束】",tradeId,taskName);
		}
	}

	@Override
	public int updateTaskLog(TaskLog tl) {
		log.info("[{}]-【更新调度日志】-【调度名称：{}】",tl.getTradeId(),tl.getTaskName());
		try{
		int num = logMapper.updateTaskLogById(tl);
		if(num == 1){
			log.info("[{}]-【更新调度日志】-【调度名称：{}】-【成功】",tl.getTradeId(),tl.getTaskName());
			return 1;
		}else{
			log.info("[{}]-【更新调度日志】-【调度名称：{}】-【失败】",tl.getTradeId(),tl.getTaskName());
			return 0;
		}
		}catch(Exception e){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();	
			log.info("[{}]-【更新调度日志】-【调度名称：{}】-【异常】",tl.getTradeId(),tl.getTaskName());
			return 0;
		}finally{
			log.info("[{}]-【更新调度日志】-【调度名称：{}】-【结束】",tl.getTradeId(),tl.getTaskName());
		}
	}
	


	
}
